<?php


namespace App\Service;

use App\Constants\XZXResponseCode;
use App\Utils\CustomUtils;
use Hyperf\Utils\Context;
use Psr\Http\Message\ServerRequestInterface;
use Swlib\SaberGM;

/**
 * Class CommonBankService
 * 银行网关相关
 *
 * @package App\Service
 */
class CommonBankService {

    protected const TIME_OUT = 20;

    const URI_PAY = '/api/bankPay/doPay';

    const URI_TRANSFER = '/api/bankPay/doTransfer';

    const URI_CHANNEL_COLLECTION_MCH_CONN_BIND = '/admin/api/channelCollectionMchConn/setBind';

    const URI_SETTLE_CURRENCY_LIST = '/admin/api/channelCollection/settleCurrencyList';

    /**
     * 支付下单
     *
     * @param $params
     *
     * @return mixed
     * @throws \Exception
     */
    public function doPay ($params) {

        $result = null;
        try {

            $api    = env('BANK_GATEWAY_URL').self::URI_PAY;
            $result = SaberGM::post($api, $params, ['timeout' => self::TIME_OUT])->getBody()->getContents();

        } catch (\Throwable $e) {
        }

        if (!$result) {
            $result = CustomUtils::buildError(trans('pay.bank.no_response'), XZXResponseCode::FAILED_CODE, ['isBankFail' => false]);
        }
        else {
            $result = json_decode($result, true);
        }

        if (CustomUtils::isError($result)) {
            $result['message'] = sprintf('[%s] %s', trans('pay.bank.log_label'), $result['message']);
        }

        return $result;
    }

    /**
     * 转帐
     *
     * @param $params
     *
     * @return mixed
     * @throws \Exception
     */
    public function doTransfer ($params) {

        $result = null;
        try {

            $api = env('BANK_GATEWAY_URL').self::URI_TRANSFER;

            $result = SaberGM::post($api, $params, ['timeout' => self::TIME_OUT])->getBody()->getContents();

        } catch (\Throwable $e) {
        }

        if (!$result) {
            throw new \Exception("请求调用银行网关模块失败");
        }

        $result = json_decode($result, true);

        return $result;
    }

    public function doRefund ($params) {

        // TODO 对接银行网关退款

        return CustomUtils::buildSuccess([], 'TODO 对接银行网关');
    }

    /**
     * @param $params
     *
     * @return mixed|null|string
     * @throws \Exception
     * 商户关联网关通道集合
     */
    public function channelCollectionMchConn ($params) {
        $result = null;
        try {
            $api           = env('BANK_GATEWAY_URL').self::URI_CHANNEL_COLLECTION_MCH_CONN_BIND;
            $request       = Context::get(ServerRequestInterface::class);
            $Authorization = $request->getHeader("Authorization")[0];
            $result        = SaberGM::post($api, $params, [
                'timeout' => self::TIME_OUT,
                'header'  => ['Authorization' => $Authorization],
            ])->getBody()->getContents();
        } catch (\Throwable $e) {
        }
        if (!$result) {
            throw new \Exception("请求调用银行网关模块失败");
        }
        $result = json_decode($result, true);
        return $result;
    }

    /**
     * @param $params
     *
     * @return mixed|null|string
     * @throws \Exception
     * 网关通道集合里面网关的结算币种列表
     */
    public function channelCollectionSettleCurrencyList ($params) {
        $result = null;
        try {
            $api           = env('BANK_GATEWAY_URL').self::URI_SETTLE_CURRENCY_LIST;
            $request       = Context::get(ServerRequestInterface::class);
            $Authorization = $request->getHeader("Authorization")[0];
            $result        = SaberGM::post($api, $params, [
                'timeout' => self::TIME_OUT,
                'header'  => ['Authorization' => $Authorization],
            ])->getBody()->getContents();
        } catch (\Throwable $e) {
        }
        if (!$result) {
            throw new \Exception("请求调用银行网关模块失败");
        }
        $result = json_decode($result, true);
        return $result;
    }

}
